/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package HibernateLayer;

import POJOs.SSessionAllocation;
import java.util.Iterator;
import java.util.List;
import java.util.TimerTask;

/**
 *
 * @author fabiofurlan
 */
public class VSessionTimerTask extends TimerTask {

    public void run(){
        //Farsi passare da VGetHibernateSession la lista delle sessioni allocate
        Iterator<SSessionAllocation> sessionItera=VGetHibernateSession.getMainHSession().mainHSession.iterator();
        List<SSessionAllocation> allocatedSessions=new VSessionAllocationServices().list();
        Iterator<SSessionAllocation> allocatedSessionItera=allocatedSessions.iterator();
        SSessionAllocation aux1=null;
        SSessionAllocation aux2=null;

        while (sessionItera.hasNext()) {
            //esegui il controllo sugli oggetti
            aux1=sessionItera.next();
            allocatedSessionItera=allocatedSessions.iterator();
            while (allocatedSessionItera.hasNext()) {
                aux2=allocatedSessionItera.next();
                //Se stiamo comparando gli stessi SSessionAllocation
                if(aux1.index==aux2.index){
                    //controllo i timestamps e se sono uguali......
                    if(aux1.lastAccess!=null && aux2.lastAccess!=null && aux1.lastAccess.equals(aux2.lastAccess)){
                        //Deallocazione della risorsa
                        VGetHibernateSession.getMainHSession().deallocateSession(aux1.index);
                    }
                }
            }
        }
    }
}
